package com.bp.api.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.common.enums.CommonCode;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.common.enums.SystemStatusEnum;
import com.idp.biz.bpuser.entity.BpUser;
import com.idp.biz.bpuser.service.BpUserService;
import com.idp.biz.bpwagecurve.entity.BpWageCurve;
import com.idp.biz.bpwagecurve.service.BpWageCurveService;

@RestController
@RequestMapping(value = "/api/wageCurve/", method = RequestMethod.POST)
public class WageCurveController {
    private static Logger logger = LoggerFactory.getLogger(WageCurveController.class);

    @Autowired
    private BpUserService userService;
    @Autowired
    BpWageCurveService wageCurveService;
    
    @Autowired
	private StringRedisTemplate redis;

    /**
     * 用户曲线列表
     */
    @CrossOrigin
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        JSONObject json = new JSONObject();
        try {
        	String token = request.getParameter("token");
            if(StringUtils.isEmpty(token)){
				json.put("code", SystemStatusEnum.PARAMETER_CANNOT_BE_EMPTY.getValue());
				json.put("message", SystemStatusEnum.PARAMETER_CANNOT_BE_EMPTY.getName());
				return json.toString();
			}
            String isLogin = redis.opsForValue().get(token);
			if(StringUtils.isEmpty(isLogin)){
				json.put("code", SystemStatusEnum.TOKEN_FAILURE.getValue());
				json.put("message", SystemStatusEnum.TOKEN_FAILURE.getName());
				return json.toString();
			}
			BpUser user = userService.getByToken(token);
			if (user == null) {
				json.put("code", SystemStatusEnum.USER_IS_NULL.getValue());
				json.put("message", SystemStatusEnum.USER_IS_NULL.getName());
				return json.toJSONString();
			}
			
            BpWageCurve wageCurve = new BpWageCurve();
            wageCurve.setUserId(user.getId());
            wageCurve.setSymbol(CommonCode.Y.getValue());
            List<BpWageCurve> dataList = wageCurveService.findBySearch(wageCurve);
            json.put("data", dataList);
			json.put("code", SystemStatusEnum.SUCCESS.getValue());
			json.put("message", SystemStatusEnum.SUCCESS.getName());
			return json.toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            json.put("code", SystemStatusEnum.SERVERERROR.getValue());
            json.put("message", SystemStatusEnum.SERVERERROR.getName());
        }
        return json.toString();
    }
    
    /**
     * 保存录入薪酬记录
     */
    @CrossOrigin
    @RequestMapping("/save")
    public String save(HttpServletRequest request) {
    	JSONObject json = new JSONObject();
    	try {
    		String token = request.getParameter("token");
    		String wageTime = request.getParameter("wageTime");
    		String event = request.getParameter("event");
    		String money = request.getParameter("money");
    		String amplitude = request.getParameter("amplitude");
    		String position = request.getParameter("position");
            if(StringUtils.isEmpty(token)){
				json.put("code", SystemStatusEnum.PARAMETER_CANNOT_BE_EMPTY.getValue());
				json.put("message", SystemStatusEnum.PARAMETER_CANNOT_BE_EMPTY.getName());
				return json.toString();
			}
            String isLogin = redis.opsForValue().get(token);
			if(StringUtils.isEmpty(isLogin)){
				json.put("code", SystemStatusEnum.TOKEN_FAILURE.getValue());
				json.put("message", SystemStatusEnum.TOKEN_FAILURE.getName());
				return json.toString();
			}
			BpUser user = userService.getByToken(token);
			if (user == null) {
				json.put("code", SystemStatusEnum.USER_IS_NULL.getValue());
				json.put("message", SystemStatusEnum.USER_IS_NULL.getName());
				return json.toJSONString();
			}
    		
    		BpWageCurve wageCurve = new BpWageCurve();
    		wageCurve.setUserId(user.getId());
            wageCurve.setWageTime(wageTime);
            wageCurve.setEvent(event);
    		wageCurve.setMoney(money);
    		wageCurve.setAmplitude(amplitude);
    		wageCurve.setPosition(position);
    		wageCurve.setCreateTime(new Date());
    		wageCurve.setSymbol(CommonCode.Y.getValue());
    		wageCurveService.add(wageCurve);
    		json.put("code", SystemStatusEnum.SUCCESS.getValue());
			json.put("message", SystemStatusEnum.SUCCESS.getName());
    	} catch (Exception e) {
    		logger.error(e.getMessage(), e);
    		json.put("code", SystemStatusEnum.SERVERERROR.getValue());
    		json.put("message", SystemStatusEnum.SERVERERROR.getName());
    	}
    	return json.toString();
    }
	
    /**
     * 删除薪酬记录
     */
	@CrossOrigin
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		try {
			String wageCurveId = request.getParameter("wageCurveId");
			if(StringUtils.isEmpty(wageCurveId)){
				json.put("code", SystemStatusEnum.PARAMETER_CANNOT_BE_EMPTY.getValue());
				json.put("message", SystemStatusEnum.PARAMETER_CANNOT_BE_EMPTY.getName());
				return json.toString();
			}
			BpWageCurve wageCurve = wageCurveService.getById(wageCurveId);
            if(wageCurve !=null){
            	wageCurve.setSymbol(CommonCode.N.getValue());
            	wageCurveService.update(wageCurve);
            	json.put("code", SystemStatusEnum.SUCCESS.getValue());
            	json.put("message", SystemStatusEnum.SUCCESS.getName());
            } else {
            	json.put("code", SystemStatusEnum.FAIL.getValue());
            	json.put("message", SystemStatusEnum.FAIL.getName());
            }
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("code", SystemStatusEnum.SERVERERROR.getValue());
			json.put("message", SystemStatusEnum.SERVERERROR.getName());
		}
		return json.toString();
	}
}
